package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.math.BigDecimal;
import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import org.apache.commons.lang.StringUtils;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp04Supply;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class PartsBySupplierFindQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;
        Teesp04Supply supply = (Teesp04Supply) parameters[0];
        Teesp01Part part = supply.getTeesp01Part();
        String supplierClass = supply.getTeess01Supplier()
                .getEess01SupplierClassCodeC();
        String supplierCode = supply.getTeess01Supplier()
                .getEess01SupplierCodeC();
        Timestamp dateInMax = supply.getEesp04LastUpdtS();
        Timestamp dateOutMax = supply.getTeess01Supplier().getEess01LastUpdtS();
        BigDecimal tvmModeFind = supply.getEesp04SupplyTvmAmountA();

        Timestamp dateInMin = part.getEesp01PartInDateY();
        Timestamp dateOutMin = part.getEesp01PartOutDateY();
        String prefix = part.getEesp01PartPrefixCodeC();
        String base = part.getEesp01PartBaseCodeC();
        String suffix = part.getEesp01PartSuffixCodeC();
        String partClass = part.getEesp01PartClassCodeC();
        Teesg01Plant plant = part.getEesp01PlantCodeC();

        exp = builder.get("eesp04PlantCodeC").equal(plant);
        if (tvmModeFind != null) {
            if (tvmModeFind.compareTo(new BigDecimal(1)) == 0)
                exp = exp.and(builder.get("eesp04SupplyTvmAmountA").notNull());
            if (tvmModeFind.compareTo(new BigDecimal(0)) == 0)
                exp = exp.and(builder.get("eesp04SupplyTvmAmountA").isNull());
        }
        if (!supplierClass.equalsIgnoreCase("-1"))
            exp = exp.and(builder.get("teess01Supplier").get(
                    "eess01SupplierClassCodeC").equal(supplierClass));
        if (supplierCode != null && !StringUtils.isBlank(supplierCode)) {
            exp = exp.and(builder.get("teess01Supplier").get(
                    "eess01SupplierCodeC").equal(supplierCode));
        }
        if (dateInMin != null) {
            exp = exp.and(builder.get("teesp01Part").get("eesp01PartInDateY")
                    .between(dateInMin, dateInMax));
        }
        if (dateOutMin != null) {
            exp = exp.and(builder.get("teesp01Part").get("eesp01PartOutDateY")
                    .between(dateOutMin, dateOutMax));
        }
        if (!partClass.equalsIgnoreCase("-1"))
            exp = exp.and(builder.get("teesp01Part")
                    .get("eesp01PartClassCodeC").equal(partClass));
        if (prefix != null) {
            prefix += "%";
            exp = exp.and(builder.get("teesp01Part").get(
                    "eesp01PartPrefixCodeC").toChar().like(prefix));
        }
        if (base != null) {
            base += "%";
            exp = exp.and(builder.get("teesp01Part").get("eesp01PartBaseCodeC")
                    .toChar().like(base));
        }
        if (suffix != null) {
            suffix += "%";
            exp = exp.and(builder.get("teesp01Part").get(
                    "eesp01PartSuffixCodeC").toChar().like(suffix));
        }

        return exp;
    }

}
